package com.lanou.tipsy.adapter.singer;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lanou.tipsy.R;
import com.lanou.tipsy.entity.singer.singerList.SingerRank;

import java.util.ArrayList;
/**
 * Created by FengJun on 15/10/22.
 * 歌手TOP100适配器
 */
public class TopAdapter extends BaseAdapter {
    private ArrayList<SingerRank> datas;
    private Context context;
    private LayoutInflater inflater;

    public TopAdapter(ArrayList<SingerRank> datas, Context context) {
        this.datas = datas;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return datas != null && datas.size() > 0 ? datas.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_singer_top, null);
            viewHolder = new ViewHolder();
            viewHolder.imageView = (SimpleDraweeView) convertView.findViewById(R.id.draw_top_100);
            viewHolder.textView = (TextView) convertView.findViewById(R.id.tv_top_100);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        SingerRank rank = (SingerRank) getItem(position);
        String url = rank.getPic_url();//解析图片url
        if (url != null) {
            Uri uri = Uri.parse(url);
            viewHolder.imageView.setImageURI(uri);
        }
        viewHolder.textView.setText(rank.getSinger_name());
        return convertView;
    }

    private class ViewHolder {
        private SimpleDraweeView imageView;
        private TextView textView;
    }
}
